#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'jiahui'

import sys


import urllib,urllib2,codecs
import os,time ,threading,csv,shutil,datetime
import glob

import Analyze.network as network
import Analyze.AnalyzeStock as AS

LatestDataDate=None
CheckDate=None
CheckTime=None
date_data=None

UpdateTime="15-30"
NotUpdateTime="05-30"

import urllib2

def update():
    global CheckDate
    global CheckTime
    global UpdateTime
    global NotUpdateTime
    global date_data
    
    nowdate=time.strftime( '%Y-%m-%d', time.localtime() )
    nowtime=time.strftime( '%H-%M', time.localtime() )    
    a=nowtime>UpdateTime
    
    if not CheckDate:
        a=True
        filename ='%s/CheckDate' % (os.path.split(os.path.realpath(__file__))[0])    
        if os.path.exists(filename):        
            date_datafile = file(filename, 'rb')
            date_data=date_datafile.read()
            CheckDate=date_data.split(':')[0]
            CheckTime=date_data.split(':')[1]
            date_datafile.close()         
        
    b=nowdate>CheckDate or (nowdate==CheckDate and CheckTime<UpdateTime)
    if a and b:
        Check()
        CheckDate=nowdate
        CheckTime=nowtime 
        if os.path.exists(filename):        
                    date_datafile = file(filename, 'wb')
                    date_datafile.write(CheckDate+":"+CheckTime)
                    date_datafile.close()                
            
def Check():
    global LatestDataDate
    
    strHtml = urllib2.urlopen('http://hq.sinajs.cn/list=sh000001').read()
    latestDate=strHtml.split(',')[-3] 
    if latestDate>LatestDataDate:
        print("newest data date ="+latestDate+"    ")
        downloadNewData()
        suc=addData(latestDate)
        print("add="+str(suc))
        #learnNewData()
        if suc:
            learnNewData()
        LatestDataDate=latestDate
    
    
def downloadNewData():
    newdatadir ='%s/NewData' % (os.path.split(os.path.realpath(__file__))[0])
    if os.path.exists(newdatadir):
        shutil.rmtree(newdatadir)
    if not os.path.exists(newdatadir):
        os.makedirs(newdatadir)    

    savedir ='%s/SourceData/data' % (os.path.split(os.path.realpath(__file__))[0])
    files=glob.glob( savedir + os.sep + '*' )    
    for f in files:
        if f[-3:]!='csv':
            continue
        stockcode=f[-13:-7]
        prefix=f[-6:-4]
        if prefix=='ss':
            prefix="sh"
        url = 'http://download.finance.yahoo.com/d/quotes.csv?s=%s.%s&f=ohgl1vq' % (stockcode,prefix)
        print(url)
        savefile='%s/NewData/%s.%s.csv' % (os.path.split(os.path.realpath(__file__))[0],stockcode,prefix)
        try:
            temfile = urllib2.urlopen(url) 
            data = temfile.read() 
            with open(savefile, "wb") as code:     
                code.write(data)
                SendNewData(data)
        except BaseException as e:
            pass
        finally:
            a=0
        
def readNewDate(filename,date):
    if os.path.exists(filename):
        csvfile = codecs.open(filename, 'rU', 'utf-8')
        reader = csv.reader(csvfile)
        
        ret=None
        
        for Open,High,Low,Close,Volume,Date in reader:
            Date=date 
            ret=[Date,Open,High,Low,Close,Volume,Close]            
        csvfile.close() 
        
        return ret


def addData(datadate):
    ret=False
    datadir ='%s/SourceData/data' % (os.path.split(os.path.realpath(__file__))[0])
    newdatadir ='%s/NewData/' % (os.path.split(os.path.realpath(__file__))[0])
    files=glob.glob( datadir + os.sep + '*' ) 
    for f in files:
        if f[-3:]!='csv':
            continue
        if os.path.exists(f):
            csvfile = codecs.open(f, 'rU', 'utf-8')
            reader = csv.reader(csvfile)
            
            onestockdata=[]
            newdata=readNewDate(newdatadir+f[-13:],datadate)
            if newdata:
                #print(newdata,"---")
                onestockdata.append(newdata)
            else:
                a=0
                #print(newdatadir+f[-13:],"|||")
            
            for Date,Open,High,Low,Close,Volume,AdjClose in reader:
                if reader.line_num==1:
                    continue
                if Date>=datadate:
                    print(f[-13:],"Date="+Date+"    datadate="+datadate)
                    return ret                
                daydata=[Date,Open,High,Low,Close,Volume,AdjClose]
                onestockdata.append(daydata)
                
            csvfile.close() 
            
            if len(onestockdata)>1:
                ret=True
                csvfile = file(f, 'wb')
                writer = csv.writer(csvfile)
                writer.writerow(['Date','Open','High','Low','Close','Volume','AdjClose'])
                writer.writerows(onestockdata)                
                csvfile.close()                
                
    return ret

def learnNewData():
    nowdate=time.strftime( '%Y-%m-%d', time.localtime() )
    
    datadir ='%s/SourceData/data' % (os.path.split(os.path.realpath(__file__))[0])
    netdir ='%s/Analyze/Net/' % (os.path.split(os.path.realpath(__file__))[0])
    files=glob.glob( datadir + os.sep + '*' )    
    for f in files:   
        if f[-3:]!="csv":
            continue
        #print(f)
        #print(netdir+f[-13:])
        result=AS.PredictNewest(f, netdir+f[-13:])
        SendPredictReuslt(nowdate,f,result)
        
def SendPredictReuslt(date,filename,result):
    print("SendPredictReuslt",date,filename[-13:],result)

def SendNewData(data):
    print("SendNewData",data)

def addRelearnList(stockcode):
    pass
    

#------------------------------------------------------------------------------------------------
if __name__=='__main__':
    update()
    print('u2')
    update()

        
 
